
"use strict";

angular.module('app.user').controller('AllAdminCtrl', function ($scope,$http,$rootScope,$cookieStore,userHttpS) {
   	//配置分页基本参数
	$scope.paginationConf = {
	    currentPage: 1,
	    itemsPerPage: 10,
	    pagesLength: 5,
	    totalItems: 20,
	    perPageOptions: [10, 20, 30, 40, 50],

	    onChange: function () {
	    }
	};

	//角色
	// $http.get(baseurls+"/list_nopage/role").then(function(res){
	// 	if(res.data.ret == 1){
	// 		// console.log(res.data.info);
	// 		$scope.roles = res.data.info;
	// 	}
	// })
   	//1.用户列表
	$scope.select = function(){
		// $http.get(baseurls+"/list_nopage/user").then(function(res){
		// 	if(res.data.ret == 1){
		// 		// console.log(res.data.info);
		// 		$scope.users = res.data.info;
		// 		$scope.paginationConf.totalItems = res.data.info.length;
		// 	}
		// })
		$http.post(baseurls+"/list/select/user", $scope.paginationConf).then(function(res){
            // console.log(res.data)
            if(res.data.ret == 1){
                $scope.users = res.data.rows;
				$scope.paginationConf.totalItems = res.data.records;
				$http.get(baseurls+"/select_idname/role").then(function(res1){
					// console.log(res1.data);
					if(res1.data.ret == 1){
						$scope.roles = res1.data.info;
						// console.log($scope.roles)
						for(let i=0;i<$scope.users.length;i++){
							for(let j=0;j<$scope.roles.length;j++){
								if($scope.roles[j].id == parseInt($scope.users[i].roleid)){
									$scope.users[i].role_name = $scope.roles[j].name;
								}
							}
						}
					}
				})
            }
            
        })
	}
	$scope.select();

	//权限: 超级管理员添加管理员
    $http.get(baseurls+"/session_user",{withCredentials:true}).then(function(res){
        // console.log(res.data.user)
        $http.get(baseurls+"/detail/user?id=="+res.data.user,{withCredentials:true}).then(function(response){
            if(response.data.ret == 1){
                if(response.data.info.fid == 0){//超级管理员
                    $scope.admin = true;
                }
            }
        })
    })
	//2.添加用户
	$scope.insert = {};
	$scope.insertdata = function () {
		if($scope.admin){
			$scope.insert.fid = 1;
		}else{
			$scope.insert.fid = $cookieStore.get("user");
		}
		$scope.insert.pass = "111111";
		$scope.insert.timefrom = getNowFormatDate();
		$http.post(baseurls+"/data/user",$scope.insert).then(function(res){
			if(res.data.ret == 1){
				// console.log($scope.insert);
				new Notify('success','添加用户成功');
			}else{
				new Notify('fail','添加用户失败');
			}
			$("#addModal").modal("hide");
			$scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.select);//更新列表
		})
	}
	//3.编辑用户
	$scope.editdata = {};
	$scope.showEditDevice = function (data) {

	    $scope.editdata.id = data.id;
	    $scope.editdata.name = data.name;
	    $scope.editdata.clientname = data.clientname;
	    $scope.editdata.tele = data.tele;
	    $scope.editdata.roleid = data.roleid;

	    $('#editModal').modal('show');
	}
	$scope.updatedata = function () {
		$http.put(baseurls+"/data/user?id=="+$scope.editdata.id,$scope.editdata).then(function(res){
			if(res.data.ret == 1){
				new Notify('success','用户信息修改成功');
			}else{
				new Notify('fail','用户信息修改失败');
			}
			$("#editModal").modal("hide");
			$scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.select);
		})
	}
	//4.删除用户
	$scope.delid = {};
	$scope.showDelDialog = function (id) {
	    $scope.delid = id;
	    $('#showDelDialog').modal('show');
	}
	$scope.deldevice = function () {
		$http.delete(baseurls+"/data/user?id=="+$scope.delid).then(function(res){
			if(res.data.ret == 1){
				new Notify('success','删除用户成功');
			}else{
				new Notify('fail','删除用户失败');
			}
			$('#showDelDialog').modal('hide');
			$scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.select);
		})		
	}

});